package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.SpendingReductionActivities;
import com.example.mall.dto.SpendingReductionActivitiesDto;
import com.example.mall.mapper.SpendingReductionActivitiesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SpendingReductionActivitiesQuery;
import com.example.mall.service.ISpendingReductionActivitiesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 满减活动表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class SpendingReductionActivitiesServiceImpl extends ServiceImpl<SpendingReductionActivitiesMapper, SpendingReductionActivities> implements ISpendingReductionActivitiesService {

    @Autowired
    private SpendingReductionActivitiesMapper spendingReductionActivitiesMapper;

    @Override
    public PageDTO<SpendingReductionActivitiesDto> queryPage(SpendingReductionActivitiesQuery spendingReductionActivitiesQuery) {
        if (spendingReductionActivitiesQuery == null) {
            spendingReductionActivitiesQuery = new SpendingReductionActivitiesQuery();
        }
        Page<SpendingReductionActivities> page = spendingReductionActivitiesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SpendingReductionActivities> cartPage = lambdaQuery()
                .like(spendingReductionActivitiesQuery.getActivityId()!= null, SpendingReductionActivities::getActivityId, spendingReductionActivitiesQuery.getActivityId())
                .like(spendingReductionActivitiesQuery.getDiscountAmount()!= null, SpendingReductionActivities::getDiscountAmount, spendingReductionActivitiesQuery.getDiscountAmount())
                .like(spendingReductionActivitiesQuery.getMinAmount()!= null, SpendingReductionActivities::getMinAmount, spendingReductionActivitiesQuery.getMinAmount())
                .like(spendingReductionActivitiesQuery.getStatus()!= null, SpendingReductionActivities::getStatus, spendingReductionActivitiesQuery.getStatus())
                .like(spendingReductionActivitiesQuery.getName()!= null, SpendingReductionActivities::getName, spendingReductionActivitiesQuery.getName())
                .apply(spendingReductionActivitiesQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        spendingReductionActivitiesQuery.getStartTime())
                .apply(spendingReductionActivitiesQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        spendingReductionActivitiesQuery.getEndTime())
                .orderByDesc(SpendingReductionActivities::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SpendingReductionActivitiesDto.class);
    }

    @Override
    public void insert(SpendingReductionActivitiesDto spendingReductionActivitiesDto) {
        SpendingReductionActivities spendingReductionActivities = new SpendingReductionActivities();
        CopyUtils.copyProperties(spendingReductionActivitiesDto, spendingReductionActivities);
        spendingReductionActivities.setActivityId(CodeGenerateUtils.generateUniqueId());
        spendingReductionActivities.setCreateDate(new Date());
        spendingReductionActivities.setUpdateDate(new Date());
        spendingReductionActivities.setStatus(String.valueOf(BaseConstants.NOT_USED));
        spendingReductionActivitiesMapper.insert(spendingReductionActivities);
    }

    @Override
    public void update(SpendingReductionActivitiesDto spendingReductionActivitiesDto) {
        AssertUtil.isNotNull(spendingReductionActivitiesDto.getActivityId(), "activityId不能为空");
        SpendingReductionActivities spendingReductionActivities = spendingReductionActivitiesMapper.selectById(spendingReductionActivitiesDto.getActivityId());
        AssertUtil.isNotNull(spendingReductionActivities, "活动不存在");
        CopyUtils.copyProperties(spendingReductionActivitiesDto, spendingReductionActivities);
        spendingReductionActivities.setUpdateDate(new Date());
        spendingReductionActivitiesMapper.updateById(spendingReductionActivities);
    }

    @Override
    public void delete(Long activityId) {
        AssertUtil.isNotNull(activityId, "activityId不能为空");
        SpendingReductionActivities spendingReductionActivities = spendingReductionActivitiesMapper.selectById(activityId);
        AssertUtil.isNotNull(spendingReductionActivities, "活动不存在");
        spendingReductionActivities.setStatus(String.valueOf(BaseConstants.EXPIRED));
        spendingReductionActivities.setUpdateDate(new Date());
        spendingReductionActivitiesMapper.updateById(spendingReductionActivities);
    }

    @Override
    public void deleteBatch(List<Long> activityIds) {
        AssertUtil.isNotNull(activityIds, "activityIds不能为空");
        List<SpendingReductionActivities> spendingReductionActivities = spendingReductionActivitiesMapper.selectBatchIds(activityIds);
        AssertUtil.isNotNull(spendingReductionActivities, "活动不存在");
        for (SpendingReductionActivities spendingReductionActivity : spendingReductionActivities) {
            spendingReductionActivity.setStatus(String.valueOf(BaseConstants.EXPIRED));
            spendingReductionActivity.setUpdateDate(new Date());
            spendingReductionActivitiesMapper.updateById(spendingReductionActivity);
        }
    }

    @Override
    public List<SpendingReductionActivities> querySpendingReductionActivitiesList() {
        return this.list();
    }

    @Override
    public SpendingReductionActivities querySpendingReductionActivitiesById(Long activityId) {
        AssertUtil.isNotNull(activityId, "activityId不能为空");
        return spendingReductionActivitiesMapper.selectById(activityId);
    }
}
